#首先要在  C:\Users\suyujie\Desktop\软件测试\web测试\中央财务系统环境搭建\中央财务系统环境搭建-windows-new
#cmd进入命令行 运行 java -jar finance.jar
#打开浏览器在90端口访问
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import wait
from selenium.webdriver.support import expected_conditions as EC
from WebAppTest.WebTest.度小满金融测试.mysql_utills import Mysql
"""
登录度小满金融 
username str 用户名
password str 密码
"""
class Duxiaoma:
    def login(self,username:str,password:str):

        driver = webdriver.Chrome()
        driver.implicitly_wait(10)
        driver.get("http://localhost:90/")
        # time.sleep(1)
        item0 = wait.WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,'//*[@placeholder="请输入您的用户名"]')))
        item0.send_keys(f'{username}')
        # driver.find_element(By.XPATH,'//*[@placeholder="请输入您的用户名"]').send_keys(f'{username}')
        # time.sleep(1)
        driver.find_element(By.XPATH,'//*[@placeholder="请输入密码"]').send_keys(f'{password}')
        # time.sleep(1)
        driver.find_element(By.XPATH,'//*[@id="login_btn"]').click()
        # time.sleep(2)
        return driver
    # def login(self,username:str,password:str):
    #     driver = webdriver.Chrome()
    #     driver.get("http://localhost:90/")
    #     time.sleep(1)
    #     driver.find_element(By.XPATH,'//*[@placeholder="请输入您的用户名"]').send_keys(f'{username}')
    #     time.sleep(1)
    #     driver.find_element(By.XPATH,'//*[@placeholder="请输入密码"]').send_keys(f'{password}')
    #     time.sleep(1)
    #     driver.find_element(By.XPATH,'//*[@id="login_btn"]').click()
    #     time.sleep(2)
    #     return driver
    """
    注册度小满金融
    """
    def register(self,username,password):
        driver = webdriver.Chrome()
        driver.implicitly_wait(10)
        driver.get("http://localhost:90/")
        time.sleep(1)
        item3 = wait.WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a/text()')))
        item3.click()
        # driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a/text()').click()
        # time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]').click()
        # time.sleep(1)
        driver.find_element(By.XPATH,'//*[@placeholder="请输入您的用户名"]').send_keys(f'{username}')
        # time.sleep(1)
        driver.find_element(By.XPATH, '//*[@placeholder="请输入密码"]').send_keys(f'{password}')
        # time.sleep(1)
        driver.find_element(By.XPATH, '//*[@placeholder="请重复密码"]').send_keys(f'{password}')
        # time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="login_btn"]').click()
        # time.sleep(2)

    # login('lisi','123456')
    # register('aaa','1211')
    """
    需新增用户
    """
    def adduser(self, username1,password1,username,password,realname,phonenum,email):
        #调用登录方法
        driver = self.login(username1,password1)
        time.sleep(1)
        #登陆完成后，进入到主页面，点击用户信息管理
        driver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        # time.sleep(1)
        #点击用户信息
        driver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]/a').click()
        # time.sleep(1)#
        #点击新增按钮
        driver.find_element(By.XPATH,'//*[@id="user_add_modal_btn"]').click()
        # time.sleep(1)
        # 输入账户名
        item2 = wait.WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,'//*[@id="username_add_input"]')))
        item2.send_keys(f'{username}')
        # driver.find_element(By.XPATH,'//*[@id="username_add_input"]').send_keys(f'{username}')
        # time.sleep(1)
        #输入密码
        driver.find_element(By.XPATH, '//*[@id="password_add_input"]').send_keys(f'{password}')
        # time.sleep(1)
        #输入真实姓名
        driver.find_element(By.XPATH, '//*[@id="realname_add_input"]').send_keys(f'{realname}')
        # time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="phone_add_input"]').send_keys(f'{phonenum}')
        # time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="email_add_input"]').send_keys(f'{email}')
        # time.sleep(1)
        driver.find_element(By.XPATH,'//*[@id="user_save_btn"]').click()
        #退出浏览器
        driver.quit()

        # time.sleep(2)
        data = Mysql('finance', 'root', 'root').select(username)
        if data is not None:
            print('添加成功')
            return 1
        else:
            print('添加失败')
            return 0

    # adduser('admin','123456','lisi1','hfwiehf','李磊','13462416729','zhangsan@qq.com')
    """
    删除用户
    """
    def deleteuser(self,username1,password1,username):
        # 调用登录方法
        driver = self.login(username1, password1)
        driver.implicitly_wait(10)
        # 登陆完成后，进入到主页面，点击用户信息管理
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        # time.sleep(1)
        # 点击用户信息
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]/a').click()
        # time.sleep(1)
        #点击删除
        driver.find_element(By.XPATH, f'//button[@delete-name="{username}"]').click()
        time.sleep(1)
        # 弹窗中点击确认
        item1 = wait.WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]')))
        item1.click()

        # driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        # time.sleep(2)
        # 退出浏览器
        driver.quit()

        if not Mysql('finance','root','root').select(username):
            print('删除成功')
            return 1
        else:
            print('删除失败')
            return 0
    # adduser('admin', '123456', 'lisi1', 'hfwiehf', '李磊', '13462416729', 'zhangsan@qq.com')
    # deleteuser('admin','123456','lisi1')
    """
    修改用户，只能修改真实姓名，手机号，邮箱
    """
    def update(self,username1,password1,userid,realname,phonenum,email):
        # 调用登录方法
        driver = self.login(username1, password1)
        data1 = Mysql('finance','root','root').select(userid)
        # 登陆完成后，进入到主页面，点击用户信息管理
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(1)
        # 点击用户信息
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]/a').click()
        time.sleep(2)
        #点击对应用户的编辑
        # driver.find_element(By.XPATH,f'//button[@update-id="{userid}"]')
        #显式等待
        wait.WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,f'//button[@update-id="{userid}"]'))).click()
        time.sleep(1)
        #输入真实姓名
        element_name = driver.find_element(By.XPATH,'//*[@id="realname_update_input"]')
        element_name.clear()
        element_name.send_keys(realname)
        time.sleep(1)
        #输入手机号
        element_phonenum = driver.find_element(By.XPATH,'//*[@id="phone_update_input"]')
        element_phonenum.clear()
        element_phonenum.send_keys(phonenum)
        time.sleep(1)
        #输入邮箱
        element_email = driver.find_element(By.XPATH,'//*[@id="email_update_input"]')
        element_email.clear()
        element_email.send_keys(email)
        time.sleep(1)
        #点击更新
        driver.find_element(By.XPATH,'//*[@id="user_update_btn"]').click()
        time.sleep(1)
        #点击确认   不够稳定易出错
        driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button')
        time.sleep(1)

        # 退出浏览器
        driver.quit()

        data2 = Mysql('finance', 'root', 'root').select(userid)
        print(data1)
        print(data2)
        if data1 != data2:
            print("修改成功")
        else:
            print("修改失败")

    # update('admin', '123456', 2,'李磊', '13462416729', 'zhangsan@qq.com')
